"""
Case Type   : 资源池化系统视图
Case Name   : 默认配置下，执行switchover操作，查看系统视图gs_thread_memory_context
Create At   : 2025/2
Owner       : haomeng
Description :
    1、主备节点查看系统表gs_thread_memory_context中DMSMemContext字段是否存在
    2、主备节点查看系统表gs_session_memory_detail中DMSMemContext字段是否存在
    3、查看enable_thread_pool参数
    4、执行switchover操作切换主备节点
    5、新主备节点查看系统表gs_thread_memory_context中DMSMemContext字段是否存在
    6、新主备节点查看系统表gs_session_memory_detail中DMSMemContext字段是否存在
    7、恢复初始环境
Expect      :
    1、存在DMSMemContext相关字段及信息
    2、存在DMSMemContext相关字段及信息
    3、默认值为off
    4、切换成功
    5、存在DMSMemContext相关字段及信息
    6、存在DMSMemContext相关字段及信息
    7、环境恢复成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = [CommonSH('PrimaryDbUser'), CommonSH('Standby1DbUser')]
        self.constant = Constant()

        self.thread_pool = 'enable_thread_pool'
        self.sys_view1 = 'gs_thread_memory_context'
        self.sys_view2 = 'gs_session_memory_detail'
        self.sys_view3 = 'gs_get_thread_memctx_detail'

    def test_ss_system_view(self):
        text = '-----step1:主备节点查看系统表gs_thread_memory_context 中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql1 = f"select * from {self.sys_view1} where contextname='DMSMemContext';"
            self.log.info(sql1)
            res1 = node.execut_db_sql(sql1)
            self.log.info(res1)
            self.assertIn('(1 row)', res1, '存在异常信息')

        text = '-----step2:主备节点查看系统表gs_session_memory_detail中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql2 = f"select * from {self.sys_view2} where contextname='DMSMemContext';"
            self.log.info(sql2)
            res2 = node.execut_db_sql(sql2)
            self.log.info(res2)
            self.assertIn('(1 row)', res2, '存在异常信息')

            tid = res2.splitlines()[-2].split('|')[0].split('.')[-1].strip()
            self.log.info(tid)
            sql3 = f"select * from {self.sys_view3}({tid}, 'DMSMemContext');"
            self.log.info(sql3)
            res3 = node.execut_db_sql(sql3)
            self.log.info(res3)
            self.assertIn('ss_dms_callback.cpp', res3, '存在异常信息')

        text = '-----step3:查看enable_thread_pool参数  expect:默认值为off-----'
        self.log.info(text)

        default_value = self.user_node[0].execut_db_sql(f'show {self.thread_pool};')
        self.log.info(default_value)
        self.assertEqual('off', default_value.splitlines()[-2].strip(), '执行失败')

        text = '-----step4:执行switchover操作切换主备节点  expect:切换成功-----'
        self.log.info(text)

        switchover = self.user_node[0].exec_cm_ctl('switchover', '-n 2',
                                                   f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switchover)
        self.assertIn(self.constant.SWITCHOVER_SUCCESS_MSG, switchover, '执行失败')

        text = '-----step5:新主备节点查看系统表gs_thread_memory_context中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql4 = f"select * from {self.sys_view1} where contextname='DMSMemContext';"
            self.log.info(sql4)
            res4 = node.execut_db_sql(sql4)
            self.log.info(res4)
            self.assertIn('(1 row)', res4, '存在异常信息')

        text = '-----step6:新主备节点查看系统表gs_session_memory_detail中DMSMemContext字段是否存在  ' \
               'expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql5 = f"select * from {self.sys_view2} where contextname='DMSMemContext';"
            self.log.info(sql5)
            res5 = node.execut_db_sql(sql5)
            self.log.info(res5)
            self.assertIn('(1 row)', res5, '存在异常信息')

            tid = res5.splitlines()[-2].split('|')[0].split('.')[-1].strip()
            self.log.info(tid)
            sql6 = f"select * from {self.sys_view3}({tid}, 'DMSMemContext');"
            self.log.info(sql6)
            res6 = node.execut_db_sql(sql6)
            self.log.info(res6)
            self.assertIn('ss_dms_callback.cpp', res6, '存在异常信息')

    def tearDown(self):
        text = '-----step7:恢复初始环境  expect:恢复成功-----'
        self.log.info(text)

        restore = self.user_node[0].exec_cm_ctl('switchover', '-a')
        self.log.info(restore)
        self.assertIn(self.constant.SWITCHOVER_SUCCESS_MSG, restore, '执行失败')

        status = self.user_node[0].exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Primary Normal', status.splitlines()[-1].split('|')[0],
                      '恢复集群初始状态失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
